package homework;

public class BankAccount {
    /*
     * 设计一个BankAccount类，包含账户号(accountNumber)、账户名(accountName)和账户余额(balance)等属性。实现以下方法：
     * deposit(double amount)：存款，将金额加到余额上。
     * withdraw(double amount)：取款，从余额中减去金额。如果金额大于余额，应抛出异常。
     * getBalance()：获取账户余额。
     */
    public static void main(String[] args) {
        BankAccount a = new BankAccount("123456", "张三", 10000);
        a.deposit(10000);
        System.out.println(a.getBalance());
        //取款
        a.withdraw(10000);
        System.out.println(a.getBalance());
        a.withdraw(20000);//余额不足会抛出异常
        System.out.println(a.getBalance());
    }
    private String accountNumber;
    private String accountName;
    private double balance;
    public void deposit(double amount){
        this.balance+=amount;
        System.out.println("存入成功");
    }
    public void withdraw(double amount){
        if(this.balance<amount){
            throw new RuntimeException("余额不足");
        }else{
            this.balance-=amount;
            System.out.println("取款成功");
        }
    }
    public double getBalance(){
        return this.balance;
    }
    public BankAccount(String accountNumber, String accountName, double balance) {
        this.accountNumber = accountNumber;
        this.accountName = accountName;
        this.balance = balance;
    }
}
